package com.caimiao.miao.library.usb.logic;

import android.content.Context;

import com.caimiao.miao.library.usb.interfaces.IUsbControl;
import com.caimiao.miao.library.usb.interfaces.IUsbReadCallback;
import com.caimiao.miao.library.usb.accessory.Command;
import com.caimiao.miao.library.usb.accessory.UsbControl;
import com.caimiao.miao.library.usb.test.TestUsb1Control;
import com.caimiao.miao.library.usb.test.TestUsbControl;

import io.reactivex.Observable;

public class UsbUtil {
    public static UsbUtil instance;
    private IUsbControl mUsbControl;

    public static UsbUtil getInstance() {
        if (instance == null) {
            synchronized (UsbControl.class) {
                if (instance == null) instance = new UsbUtil();
            }
        }
        return instance;
    }

    private UsbUtil() {
    }

    public UsbUtil initUsbControl(Context context) {
        mUsbControl = new UsbControl(context);
        return this;
    }

    public UsbUtil initTestUsbControl(Context context) {
        mUsbControl = new TestUsbControl(context);
        return this;
    }

    public UsbUtil initTest1UsbControl(Context context) {
        mUsbControl = new TestUsb1Control(context);
        return this;
    }

    public UsbUtil addUsbReadCallback(IUsbReadCallback iUsbReadCallback) {
        mUsbControl.addUsbReadCallback(iUsbReadCallback);
        return this;
    }

    public Observable<Command> addTicketOut(int value) {
        addCommand(UsbCode.SET_TICKET_VALUE, value);
        return Observable.create(new TicketOnSubscribe(value));
    }

    public UsbUtil addCommand(int command, int value) {
        addCommand(command, value, 0x00);
        return this;
    }

    public UsbUtil addCommand(int command, int value, int value2) {
        Command cmd = Command.Util.getCommand(command, value, value2);
        cmd.setColor(0xffff0000);
        mUsbControl.addCommandToWriteQueue(cmd);
        return this;
    }

    public void resumeAccessory() {
        mUsbControl.ResumeAccessory();
    }

    public void destroy() {
        mUsbControl.destroy();
    }
}
